package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.Interface;
import gov.dhs.cis.soa.data.RelatedProperties;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class InterfacesReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(InterfacesReader.class);

	private final String EXT_INTERFACES_FILE_NAME = "USCIS Architecture Components - External Interface Layer.xls";
	private String SHEET_NAME = "Unique Interfaces";
	
	private String INTERFACE_COL_NUM = "B";
	private String DESC_COL_NUM = "C";
	private String TYPE_COL_NUM = "E";
	private String WIPT_COL_NUM = "F";
	private String REL_COL_NUM = "G";
	private String MATURITY_COL_NUM = "H";

	private String SYS_EE_COL_NUM = "J";
	private String AGENCY_COL_NUM = "K";
	private String ACT_COL_NUM = "L";
	
	private String L3_BUS_PROC_COL_NUM = "N";
	private String USE_CASE_COL_NUM = "O";	//added on 07/19/2010 - RSingh
	
	private String REL2SYS_COL_NUM = "P";
	private String PRIM_DATA_FLOW_COL_NUM = "Q";
	
	private String REQ_DATA_FLOW_COL_NUM = "R";
	private String RESP_DATA_FLOW_COL_NUM = "S";
	
	private String TRANS_SERV_COL_NUM = "U";
	private String IOE_AGENT_COL_NUM = "V";
	private String MEP_COL_NUM = "W";
	private String CAND_USE_ENT_SERV_COL_NUM = "AF";
	private String NEEDED_BY_COL_NUM = "AN";
	private String INTEG_HOST_NUM = "AO";
	private String DA_COL_NUM = "AQ";
	private String ASSUMP_COL_NUM = "AT";
	private String COMP_COL_NUM = "AZ";
	
	private int MAX_PROCS = 130;
		
	public SOAData extractInterfaces(SOAData soaData) {
		loger.debug("Extracting External Interfaces layer Sheet...");
		HSSFWorkbook w = openFile(EXT_INTERFACES_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<Interface> interfacesList = new ArrayList<Interface>();
		soaData.intList = interfacesList;
		for (int n = 4; n < MAX_PROCS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String interfaceName = getCellStringValue(w, row, INTERFACE_COL_NUM);
			if(Utils.isEmpty(interfaceName)) {
				loger.warn("Interface Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			String stereotype = getCellStringValue(w, row, TYPE_COL_NUM);
			if(Utils.isEmpty(stereotype)) {
				loger.warn("Interface stereo type is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			Interface iObj = new Interface();
			iObj.id = Utils.getNextId();
			iObj.name = interfaceName;
			iObj.stereotype = stereotype;
			iObj.descText = getCellStringValue(w, row, DESC_COL_NUM);
			iObj.wipt = getCellStringValue(w, row, WIPT_COL_NUM);
			iObj.release = getCellStringValue(w, row, REL_COL_NUM);
			iObj.release = Utils.convert2Release(iObj.release);
			int maturity = Utils.convert1stchar2Int(getCellStringValue(w, row, MATURITY_COL_NUM));
			iObj.maturity = maturity;
			iObj.sysextentity = getCellStringValue(w, row, SYS_EE_COL_NUM);
			iObj.agency = getCellStringValue(w, row, AGENCY_COL_NUM);
			iObj.actorusing = getCellStringValue(w, row, ACT_COL_NUM);
			iObj.l3busprocname = getCellStringValue(w, row, L3_BUS_PROC_COL_NUM);
			iObj.usecase = getCellStringValue(w, row, USE_CASE_COL_NUM);

			iObj.reltosys = getCellStringValue(w, row, REL2SYS_COL_NUM);
			iObj.dirprimdataflow = getCellStringValue(w, row, PRIM_DATA_FLOW_COL_NUM);
			iObj.reqdataflow = getCellStringValue(w, row, REQ_DATA_FLOW_COL_NUM);
			iObj.respdataflow = getCellStringValue(w, row, RESP_DATA_FLOW_COL_NUM);
			//iObj.sysextentity = getCellStringValue(w, row, SYS_EE_COL_NUM);
			iObj.serviceref = getCellStringValue(w, row, TRANS_SERV_COL_NUM);
			iObj.ioeagent = getCellStringValue(w, row, IOE_AGENT_COL_NUM);
			iObj.mep = getCellStringValue(w, row, MEP_COL_NUM);
			iObj.candUseOfEntServ = getCellStringValue(w, row, CAND_USE_ENT_SERV_COL_NUM);
			iObj.neededby = getCellStringValue(w, row, NEEDED_BY_COL_NUM);
			iObj.integHosting = getCellStringValue(w, row, INTEG_HOST_NUM);
			iObj.designAspects = getCellStringValue(w, row, DA_COL_NUM);
			iObj.assumptions = getCellStringValue(w, row, ASSUMP_COL_NUM);
			
			iObj.createDescription();
			
			String compID = getCellStringValue(w, row, COMP_COL_NUM);
			if(soaData.propsList == null)
				soaData.propsList = new ArrayList<RelatedProperties>();
			soaData.propsList.addAll(RelatedProperties.getNewInterfacesProperty(iObj, "COMP_ID", compID));

			interfacesList.add(iObj);
		}
		closeAll(w);
		return soaData;
	}
}
